using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GRE.Logics;
using GRE.DataAccess;
using GRE.Framework;

namespace GRE
{
    public partial class frmSave : Form
    {
        private char[] splitter = new char[] { ';' };

        private AWord _word = null;
        private AWord Word
        {
            get
            {
                if (_word == null)
                {
                    _word = new AWord(txtWord.Text);
                }
                return _word;
            }
            set
            {
                _word = value;
            }
        }

        public frmSave()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (_etym == null)
                {
                    _etym = new Etymology();
                    _etym.Language = txtEtymLang.Text;
                    _etym.Description = txtEtymDesc.Text;
                    EtymologyWord etymWord = new EtymologyWord(txtEtymWord.Text);
                    etymWord.Meaning = txtEtymMeaning.Text;
                    _etym.AddWord(etymWord);
                }
                if (Word.HasNoMeaning)
                {
                    Meaning mnng = new Meaning(txtMeaning.Text);
                    mnng.Usage = txtUsage.Text;
                    mnng.AddSynonyms(txtSynonyms.Text.Split(splitter));
                    mnng.AddAntonyms(txtAntonyms.Text.Split(splitter));
                    Word.AddMeaning(mnng);
                }
                Word.Etymology = _etym;
                DataAccessEngine.DictionaryWords.List.Add(Word);
                XMLHelper.SerializeToFile(DataAccessEngine.DictionaryWords);
            }
            catch
            {
                MessageBox.Show("Error, boss");
            }
        }

        private void frmSave_Load(object sender, EventArgs e)
        {
        }

        private void btnAddMoreWord_Click(object sender, EventArgs e)
        {
            Meaning mnng = new Meaning(txtMeaning.Text);
            mnng.Usage = txtUsage.Text;
            mnng.AddSynonyms(txtSynonyms.Text.Split(splitter));
            mnng.AddAntonyms(txtAntonyms.Text.Split(splitter));
            Word.AddMeaning(mnng);
        }

        Etymology _etym = null;
        private void btnAddEtymWords_Click(object sender, EventArgs e)
        {
            if (_etym == null)
            {
                _etym = new Etymology();
                _etym.Language = txtEtymLang.Text;
                _etym.Description = txtEtymDesc.Text;
            }
            EtymologyWord etymWord = new EtymologyWord(txtEtymWord.Text);
            etymWord.Meaning = txtEtymMeaning.Text;
            _etym.AddWord(etymWord);
        }
    }
}